﻿/**  版本信息模板在安装目录下，可自行修改。
* NodesLessons.cs
*
* 功 能： N/A
* 类 名： NodesLessons
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2015/4/11 14:29:37   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;//Please add references
namespace Catering_Operation
{
	/// <summary>
	/// 数据访问类:NodesLessons
	/// </summary>
	public partial class NodesLessons
	{
		public NodesLessons()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("LessonID", "T_NodesLessons"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int LessonID,int NodeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from T_NodesLessons");
			strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID");
			SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4)};
			parameters[0].Value = LessonID;
			parameters[1].Value = NodeID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Catering_Model.NodesLessons model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_NodesLessons(");
            strSql.Append("LessonID,NodeID,AddUserID,AddTime)");
			strSql.Append(" values (");
            strSql.Append("@LessonID,@NodeID,@AddUserID,@AddTime)");
			SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@AddUserID", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime)};
			parameters[0].Value = model.LessonID;
			parameters[1].Value = model.NodeID;
            parameters[2].Value = model.AddUserID;
            parameters[3].Value = model.AddTime;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Catering_Model.NodesLessons model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_NodesLessons set ");
            strSql.Append("AddUserID=@AddUserID,");
            strSql.Append("AddTime=@AddTime ");
			strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@AddUserID", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)};
            parameters[0].Value = model.AddUserID;
            parameters[1].Value = model.AddTime;
			parameters[2].Value = model.LessonID;
			parameters[3].Value = model.NodeID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int LessonID,int NodeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_NodesLessons ");
			strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4)};
			parameters[0].Value = LessonID;
			parameters[1].Value = NodeID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Catering_Model.NodesLessons GetModel(int LessonID,int NodeID)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 LessonID,NodeID,AddUserID,AddTime from T_NodesLessons ");
			strSql.Append(" where LessonID=@LessonID and NodeID=@NodeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4),
					new SqlParameter("@NodeID", SqlDbType.Int,4)};
			parameters[0].Value = LessonID;
			parameters[1].Value = NodeID;

			Catering_Model.NodesLessons model=new Catering_Model.NodesLessons();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Catering_Model.NodesLessons DataRowToModel(DataRow row)
		{
			Catering_Model.NodesLessons model=new Catering_Model.NodesLessons();
			if (row != null)
			{
				if(row["LessonID"]!=null && row["LessonID"].ToString()!="")
				{
					model.LessonID=int.Parse(row["LessonID"].ToString());
				}
				if(row["NodeID"]!=null && row["NodeID"].ToString()!="")
				{
					model.NodeID=int.Parse(row["NodeID"].ToString());
				}
                if (row["AddUserID"] != null && row["AddUserID"].ToString() != "")
				{
                    model.AddUserID = int.Parse(row["AddUserID"].ToString());
				}
                if (row["AddTime"] != null && row["AddTime"].ToString() != "")
				{
                    model.AddTime = DateTime.Parse(row["AddTime"].ToString());
				}				
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select LessonID,NodeID,AddUserID,AddTime ");
			strSql.Append(" FROM T_NodesLessons ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" LessonID,NodeID,AddUserID,AddTime ");
			strSql.Append(" FROM T_NodesLessons ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM T_NodesLessons ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_NodesLessons";
			parameters[1].Value = "UserID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetLessonNodesList(int intLessonID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT DISTINCT L.NodeID,N.Name AS NodeName FROM T_NodesLessons AS L LEFT JOIN BranchPost AS N ON N.id=L.NodeID WHERE L.LessonID = " + intLessonID);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetNothingNodesList(int intLessonID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT DISTINCT N.id AS NodeID,N.Name AS NodeName FROM BranchPost AS N ");
            strSql.Append("LEFT JOIN(SELECT NodeID FROM T_NodesLessons WHERE LessonID=" + intLessonID + ")L ON N.id=L.NodeID WHERE L.NodeID IS NULL ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int LessonID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_NodesLessons ");
            strSql.Append(" where LessonID=@LessonID ");
            SqlParameter[] parameters = {
					new SqlParameter("@LessonID", SqlDbType.Int,4)};
            parameters[0].Value = LessonID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int ViewGetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select COUNT(*) from t_NodesLessons AS N ");
            strSql.Append(" left join T_StaffLessons AS L on N.LessonID=L.LessonID ");
            strSql.Append(" where [Status]='O' " + strWhere);
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public DataSet ViewGetList(int PageSize, int PageIndex, string strWhere)
        {
            PageIndex = PageIndex < 1 ? 1 : PageIndex;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select tab.*,G.TestID,G.TestDate,G.UserID,G.UserStartDate,G.UserEndDate,G.TestPoints,G.Addre from (select N.NodeID,L.*,ROW_NUMBER() OVER(ORDER BY L.LessonID ASC) AS rowNum from t_NodesLessons AS N ");
            strSql.Append(" left join T_StaffLessons AS L on N.LessonID=L.LessonID where [Status]='O' " + strWhere + ") tab ");
            strSql.Append(" left join T_StaffLessonsLog AS G on tab.LessonID=G.LessonID AND tab.NodeID= G.NodeID ");
            strSql.Append(" WHERE rowNum BETWEEN " + (((PageIndex - 1) * PageSize) + 1) + " AND " + PageIndex * PageSize);
            return DbHelperSQL.Query(strSql.ToString());
        }

		#endregion  ExtensionMethod
	}
}

